﻿//当前窗口
var Shadowbox = window.parent.Shadowbox;
//控件集合实例
var controlsCollection;

$(document).ready(function () {
    lblKeyid.SetText($.query.get('Keyid'));
    lblUsername.SetText($.query.get('UserName'));

    var collection = [
        New(ASPxRFTextBox, ['txtDeptName', ['GroupA'], 'DeptName']),
        New(ASPxRFTextBox, ['txtDeptCode', ['GroupA'], 'DeptCode']),
        New(ASPxRFTextBox, ['txtHelpCode', ['GroupA'], 'HelpCode']),
        New(ASPxRFComboBox, ['cbDeptType', ['GroupA'], { valueField: 'DeptType'}])
    ];
    controlsCollection = New(RapidForm, [collection]); 
});
//检索
var doSearch = function () {
    doDisplay('search');
    setEnableByButtonName(['btnRefresh'], false);
}
//列出所有
var doListAll = function () {
    doDisplay('listall');
    //启用刷新按钮
    setEnableByButtonName(['btnRefresh'], true);
    //显示列表按钮被选中
    menuHeader.SetSelectedItem(menuHeader.GetItemByName('btnListAll'));
}
//刷新
var doRefresh = function () {
    $('#hfFilterCondition').val(' ');
    gvwList.PerformCallback();
}
//GridView选中全部或者取消选中全部
var doSelectOrUnselectAll = function (s, e) {
    gvwList.SelectAllRowsOnPage(s.GetChecked());
}
//执行查找
var doSubmittedToSearch = function () {
    doListAll();
    $('#hfFilterCondition').val(controlsCollection.getQueryString('GroupA'));
    gvwList.PerformCallback();
}
//重置
var doResetToSearch = function () {
    controlsCollection.reset('GroupA');
}
//为用户添加角色
var doAddRoels = function () {

    //获取选中角色的Keyid和DeptCode
    var deptKeyids = [];
    var deptCodes = [];
    gvwList.GetSelectedFieldValues('Keyid;DeptCode', function (result) {
        for (var i = 0; i < result.length; i++) {
            deptKeyids.push(result[i][0]);
            deptCodes.push(result[i][1]);
        }
        SGDemo.Permission.AddUserDept.BulkAddUserDept(lblKeyid.GetText(), lblUsername.GetText(), deptKeyids, deptCodes, function (result) {
            if (result.value) {

                $.messager.show({
                    title: '信息提示',
                    msg: '添加部门成功！'
                });
                //本弹出框页面GridView刷新
                gvwList.PerformCallback();
                //父级GridView刷新
                parent.gvwList.PerformCallback();
            } else {
                $.messager.show({
                    title: '信息提示',
                    msg: '添加部门失败！'
                });
            }
        });
    });
}
//关闭当前按钮
var doClose = function () {
    Shadowbox.close();
}
//启用或者禁用指定的按钮
var setEnableByButtonName = function (buttonName, isEnable) {
    for (var i = 0; i < buttonName.length; i++) {
        var menuItem = menuHeader.GetItemByName(buttonName[i]);
        if (menuItem) {
            menuItem.SetEnabled(isEnable);
        }
    }
}

var doPrint = function () {
    window.print();
}

var doHelp = function () {

}